package com.jgdt.demo.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * @author: luchyu
 * @date: 2015年9月25日 上午10:46:55
 */
public class FlowGridLayout extends ViewGroup{
    private int columnNum = 3;
    private int mCellWidth=0,mCellHeight=0;

    public FlowGridLayout(Context context) {
        super(context);
        this.initialize(context,null);
    }
    public FlowGridLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initialize(context,attrs);
    }
    public FlowGridLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.initialize(context,attrs);
    }
    private void initialize(Context context,AttributeSet attrs) {
        if(attrs==null) return;
        TypedArray mTypedArray=context.obtainStyledAttributes(attrs,new int[]{android.R.attr.rowCount,android.R.attr.columnCount});
        this.columnNum=mTypedArray.getInt(2,3);
        mTypedArray.recycle();
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthMode != MeasureSpec.EXACTLY) {
            throw new IllegalStateException("CellLayout can only be used in EXACTLY mode.");
        }
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //int layoutHeight = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {
            throw new IllegalStateException("CellLayout can only be used in EXACTLY mode.");
        }
        int layoutWidth = MeasureSpec.getSize(widthMeasureSpec);
        if (this.columnNum > 0) {
            mCellWidth=layoutWidth/columnNum;
        }
        mCellHeight=mCellWidth;
        int cnt=0;
        for (int i = 0; i < getChildCount(); i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() == View.GONE) {
                continue;
            }
            cnt++;
            //measureChild(child, widthMeasureSpec, heightMeasureSpec);
            measureChild(child,MeasureSpec.makeMeasureSpec(mCellWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(mCellHeight, MeasureSpec.EXACTLY));
        }
        int row=(cnt+columnNum-1)/columnNum;
        int layoutHeight=row*mCellHeight;
        setMeasuredDimension(widthMeasureSpec, MeasureSpec.makeMeasureSpec(layoutHeight, MeasureSpec.EXACTLY));
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int count = getChildCount();
        int x=0,y=0;
        for(int i=0;i<count;i++){
            final View child = getChildAt(i);
            if (child.getVisibility() == View.GONE) {
                continue;
            }
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            child.layout(x+lp.leftMargin, y+lp.topMargin, x + mCellWidth-lp.rightMargin, y + mCellHeight-lp.bottomMargin);
            x=(x+mCellWidth)%(columnNum*mCellWidth);
            if(x!=0){
                continue;
            }
            y=y+mCellHeight;
        }
    }
    @Override
    protected ViewGroup.MarginLayoutParams generateDefaultLayoutParams() {
        return new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }
    @Override
    public ViewGroup.MarginLayoutParams generateLayoutParams(AttributeSet attrs) {
        return new ViewGroup.MarginLayoutParams(getContext(), attrs);
    }
    @Override
    protected ViewGroup.MarginLayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
        if (lp instanceof ViewGroup.MarginLayoutParams) {
            return (MarginLayoutParams) lp;
        }
        return new ViewGroup.MarginLayoutParams(lp);
    }
}